//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.ComponentModel;

namespace GuidanceExplorer.Model
{
    public class FieldSearchCriteria : Criteria
    {
        private Field _field;
        private string _value;
        private SearchTerms _searchTerm;
        private Guidance _guidance;

        public FieldSearchCriteria(Field field, string value, SearchTerms searchTerm, Guidance guidance)
        {
            _field = field;
            _value = value;
            _searchTerm = searchTerm;
            _guidance = guidance;
        }

        public Guidance Guidance
        {
            get { return _guidance; }
            set { _guidance = value; }
        }

        public string Value
        {
            set { _value = value; }
            get { return _value; }
        }

        public Field Field
        {
            set { _field = value; }
            get { return _field; }
        }

        public SearchTerms SearchTerm
        {
            set { _searchTerm = value; }
            get { return _searchTerm; }
        }

        public override bool Evaluate(GuidanceItem item)
        {
            GuidanceItemFileReference gifr = item as GuidanceItemFileReference;
            if (gifr == null)
            {
                gifr = _guidance.GetGuidanceItemFileReference(item);
            }

            if (gifr != null)
            {
                PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(gifr.GetType());
                string itemsValue = null;

                //Find the property in the type.
                if (propertyDescriptors[_field.Name] != null)
                {
                    itemsValue = (string)propertyDescriptors[_field.Name].GetValue(gifr);
                }
                //If the property was not found, then use the custom data.
                else
                {
                    itemsValue = gifr[_field.Name];
                }

                switch (_searchTerm)
                {
                    case SearchTerms.Contains:
                        return (itemsValue.IndexOf(_value, StringComparison.CurrentCultureIgnoreCase) != -1);
                    case SearchTerms.Ends_With:
                        return itemsValue.EndsWith(_value, StringComparison.CurrentCultureIgnoreCase);
                    case SearchTerms.Equals:
                        return itemsValue.Equals(_value, StringComparison.CurrentCultureIgnoreCase);
                    case SearchTerms.Not_Contains:
                        return (itemsValue.IndexOf(_value, StringComparison.CurrentCultureIgnoreCase) == -1);
                    case SearchTerms.Not_Equals:
                        return !itemsValue.Equals(_value, StringComparison.CurrentCultureIgnoreCase);
                    case SearchTerms.Starts_With:
                        return itemsValue.StartsWith(_value, StringComparison.CurrentCultureIgnoreCase);
                    default:
                        return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
